package com.zd.dao;

import com.zd.entity.News;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface NewsDao extends PagingAndSortingRepository<News, Long> {

    /*
     *查询所有新闻，根据创建时间倒序排列
     * @return
     * */
    public List<News> getAllByOrderByNcreateDateDesc();

    /*
     *查询主题下的新闻
     * @param tname
     * @return
     * */
    @Query(value = "select * from news where ntid=(select tid from topic where tname =?1) order by ncreateDate desc limit 5", nativeQuery = true)
    public List<News> getAllNewsByTname(@Param("tname") String tname);

    public List<News> getAllByNtidOrderByNcreateDateDesc(Long ntid);

    News getNewsByNid(Long nid);

    int countAllBy();

    @Query(value = "select * from news order by ncreateDate desc limit ?1,?2", nativeQuery = true)
    List<News> getNewsByPage(@Param("index") int index, @Param("pageSize") int pageSize);


    /**
     * 判断某个主题下是否有新闻
     * @param ntid
     * @return
     */
    boolean existsNewsByNtid(Long ntid);

    /**
     * 删除指定新问
     * @param nid
     */
    void deleteByNid(Long nid);

}